package com.gcsoftware.web.service;

import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.web.beans.request.GetGrantRoleReq;
import com.gcsoftware.web.threads.FileUploadThread;
import com.gcsoftware.web.mapper.T_COM_SYS_LOGMAPPER;
import com.gcsoftware.web.beans.T_GW_ROLES;
import com.gcsoftware.web.mapper.T_GW_ROLEMAPPER;
import com.gcsoftware.utils.log.InvokeLog;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.utils.GenerateIDs;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * Created by qs on 2018/3/14.
 */
@Service
public class T_GW_ROLE_SERVICE {
    @Autowired
    private T_GW_ROLEMAPPER mapper ;

    @Autowired
    private T_COM_SYS_LOGMAPPER logMapper;

    @Autowired
    FileUploadService fileUploadService;

    public List<T_GW_ROLES> queryForList(String c_ROLE_TYPE,String c_ROLE_NAME){
        return mapper.queryForList(c_ROLE_TYPE,c_ROLE_NAME);
    }

    public int getMaxId(){
        String id = mapper.getMaxId();
        int maxId = 0;
        if(StringUtils.isNotBlank(id)) {
            maxId = Integer.valueOf(id.replace(GenerateIDs.ID_TYPE_ROLE_ID,""));
        }
        return maxId;
    }

    public T_GW_ROLES get(String C_ROLE_ID ,  String c_ROLE_NAME) {
        return mapper.get(C_ROLE_ID,c_ROLE_NAME);
    }


    public MapUtils insert(T_GW_ROLES role){
        if (null != get(null, role.getC_ROLE_NAME())) {
            return MapUtils.error("已存在同名的接口角色名称");
        }
        return insertRole(role);
    }
    @InvokeLog(name = "add role" , description = "新增接口角色" , logType = LogUtils.LOG_TYPE_OPERATE)
    private MapUtils insertRole(T_GW_ROLES role) {
        if (GenerateIDs.getMaxRoleId() == 0) {
            GenerateIDs.setMaxRoleId(getMaxId());
        }
        role.setC_ROLE_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_ROLE_ID));
        if (null != get(role.getC_ROLE_ID(),null)) {
            GenerateIDs.setMaxRoleId(getMaxId());
            role.setC_ROLE_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_ROLE_ID));
        }

        return  1 == mapper.insert(role) ? MapUtils.ok("新增接口角色成功"): MapUtils.error("新增接口角色失败");
    }
    @InvokeLog(name = "update role" , description = "修改接口角色" , logType = LogUtils.LOG_TYPE_OPERATE)
    private MapUtils updateRole(T_GW_ROLES role) {
        return  1 == mapper.update(role) ? MapUtils.ok("修改接口角色成功") : MapUtils.error("修改接口角色失败");
    }

    public MapUtils update(T_GW_ROLES role)
    {
        T_GW_ROLES r = get(null, role.getC_ROLE_NAME());
        if (null != r && r.getC_ROLE_NAME().equals(role.getC_ROLE_NAME())) {
            return MapUtils.error("已存在同名的接口角色名称");
        }
        return updateRole(role);
    }

    @InvokeLog(name = "delete role" , description = "删除接口角色" , logType = LogUtils.LOG_TYPE_OPERATE)
    public int delete(String c_ROLE_ID) {
        return mapper.delete(c_ROLE_ID) ;
    }

    public Map<String, List<String>> queryRoleDsiInterfs(String c_ROLE_ID) {
        List<String> interfs = mapper.queryInterfListForRole(c_ROLE_ID);
        List<String> dsList = mapper.queryDsiListForRole(c_ROLE_ID);

        Map<String , List<String>> map = new HashMap<>();
        map.put("interfs" , interfs);
        map.put("dsiLists" , dsList);
        return map;
    }

    @Transactional
    public MapUtils grantRoles(GetGrantRoleReq req) {
        if (null == req) {
            return MapUtils.error("未能获取授权信息，请刷新页面后重试!");
        }
        if (StringUtils.isBlank(req.getC_ROLE_ID())) {
            return MapUtils.error("未能获取授权的角色信息，请刷新页面后重试");
        }
        try {
            mapper.removeRoleInters(req.getC_ROLE_ID());
            mapper.removeRoleDsis(req.getC_ROLE_ID());
            if (CollectionUtils.isNotEmpty(req.getInterfList())) {
                mapper.batchInsertRoleInterfs(req.getC_ROLE_ID(), req.getInterfList());
            }
            if (CollectionUtils.isNotEmpty(req.getDsiList())) {
                mapper.batchInsertRoleDsis(req.getC_ROLE_ID(), req.getDsiList());
            }

            FileUploadThread.setService(fileUploadService);
            FileUploadThread.setRoleQue(req.getC_ROLE_ID());
            return MapUtils.ok("角色授权成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return MapUtils.error("角色授权失败");
    }

}
